<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use api\home\model\SlideItemModel;
use api\home\model\SlideModel;
use app\portal\model\PortalCategoryModel;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use cmf\controller\HomeBaseController;
use think\db\Query;
use think\facade\Lang;


class IndexController extends HomeBaseController
{
    public function index()
    {
        $search      = $this->request->param('search', '');
        $en      = $this->request->param('en', '');
        $agent      = $this->request->param('agent', '');

        $langType = '0';
        $lang = session('Lang');
        if ($lang){
            Lang::setLangSet($lang);
            if ($lang == 'us'){
                $langType = '1';
            }
        }
        if ($en =='us' ){
            $langType = '0';
            session('Lang','en-us');
            Lang::setLangSet('en-us');

        }elseif ($en =='cn' ){
            $langType = '0';
            session('Lang','zh-cn');
            Lang::setLangSet('zh-cn');
        }

        if ($agent != ''){
            session('agent',$agent);
        }



        $is_mobile = cmf_is_mobile();

        //dump(cmf_is_mobile());exit;
        // 幻灯片
        $slideModel = new SlideItemModel();
        $where = [];
        $where[] = ['status','=','1'];
        $where[] = ['slide_id','=','1'];
        $slide_list = $slideModel->where($where)->order('list_order desc')->select();

        $portalPostModel = new PortalPostModel();
        $wherePublishedTime = function (Query $query) {
            $search      = $this->request->param('search', '');
            if (!empty($search)) {
                $query->where('post.post_title', 'like', "%$search%");
            }

            $query->where('post.published_time', '>', 0)
                ->where('post.published_time', '<', time());
        };
        $categoryId = 1;
        $where = [
            'post.post_type'       => 1,
            'post.post_status'     => 1,
            'post.delete_time'     => 0,
            'relation.category_id' => $categoryId,
            'post.lang' => $langType,
        ];

        // 推荐平台
        $article = $portalPostModel->alias('post')->field('post.*')
            ->join('portal_category_post relation', 'post.id = relation.post_id')
            ->where($where)
            ->where($wherePublishedTime)
            ->limit(8)
            ->order('relation.list_order desc,update_time desc')
            ->select();
        foreach ($article as $key => &$value){
            $value['keywords_list'] = [];
            if ($value['post_keywords'] != ''){
                $value['keywords_list'] = explode(',',$value['post_keywords']);
            }
        }
        $this->assign('category1', $article);

        // 团队老师
        $categoryId = 2;
        $where = [
            'post.post_type'       => 1,
            'post.post_status'     => 1,
            'post.delete_time'     => 0,
            'relation.category_id' => $categoryId,
            'post.lang' => $langType,
        ];

        $article = $portalPostModel->alias('post')->field('post.*')
            ->join('portal_category_post relation', 'post.id = relation.post_id')
            ->where($where)
            ->where($wherePublishedTime)
            ->limit(8)
            ->order('relation.list_order desc,update_time desc')
            ->select()->toArray();
        foreach ($article as $key => &$value){
            $value['keywords_list'] = [];
            if ($value['post_keywords'] != ''){
                $value['keywords_list'] = explode(',',$value['post_keywords']);
            }
        }

        $article2 = array_chunk($article,3);

        $this->assign('category2', $article2);


        // 咨询
        $model = new PortalCategoryModel();
        $where2= [
            'parent_id'       => 3,
        ];
        $cateList = $model->where($where2)->order('list_order desc')->select();

        foreach ($cateList as &$item){
            $item['portal_list'] = [];
            $where = [
                'post.post_type'       => 1,
                'post.post_status'     => 1,
                'post.delete_time'     => 0,
                'relation.category_id' => $item['id'],
                'post.lang' => $langType,
            ];

            $article = $portalPostModel->alias('post')->field('post.*')
                ->join('portal_category_post relation', 'post.id = relation.post_id')
                ->where($where)
                ->where($wherePublishedTime)
                ->limit(10)
                ->order('relation.list_order desc,update_time desc')
                ->select()->toArray();
            foreach ($article as $key => &$value){
                $value['keywords_list'] = [];
                if ($value['post_keywords'] != ''){
                    $value['keywords_list'] = explode(',',$value['post_keywords']);
                }
            }
            $item['portal_lists'] = $article;

        }


        $this->assign('nav', 'home');
        $this->assign('search', $search);
        $this->assign('category3', $cateList);
        $this->assign('category4', $cateList);



        $this->assign('slide_list', $slide_list);

        $this->assign('slide_image', isset($slide_list[0]['image']) ? $slide_list[0]['image'] : '');


        if ($is_mobile == true){
            return $this->fetch(':mobile_index');
        }else{
            return $this->fetch(':index');
        }


    }
}
